package com.dh.cheesestrip.Widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dh.cheesestrip.R;
import com.dh.cheesestrip.Util.ScreenUtil;
import com.dh.cheesestrip.Util.StringUtil;


/**
 * Created by Mc on 16/1/24.
 */
public class TitleBar extends LinearLayout {

    private Context context;
    private int imageSize = 20;
    private final int padding8 = 8;
    private final int padding16 = 16;

    private TextView tv_center, tv_left, tv_right;
    public ImageView iv_left, iv_right;
    private LinearLayout ll_left, ll_right;


    private Drawable barBackgroundDrawable;
    private int barBackgroundColor;

    private String centerText;
    private float centerTextSize;
    private int centerTextColor;

    private String leftText;
    private float leftTextSize;
    private int leftTextColor;
    private Drawable leftImage;

    private String rightText;
    private float rightTextSize;
    private int rightTextColor;
    private Drawable rightImage;
    
    private int defaultColor= R.color.white;


    public TitleBar(Context context) {
        super(context);
        isInEditMode();
        this.context = context;
        imageSize = ScreenUtil.dip2px(context, 20);
    }

    public void setBarBackgroundDrawable(Drawable barBackgroundDrawable) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            this.setBackground(barBackgroundDrawable);
        } else {
            this.setBackgroundDrawable(barBackgroundDrawable);
        }
    }

    public void setBarBackgroundColor(int barBackgroundColor) {
        this.setBackgroundColor(barBackgroundColor);
    }

    public void setCenterText(String centerText) {
        tv_center.setText(centerText);
    }

    public void setCenterTextSize(float centerTextSize) {
        tv_center.setTextSize(centerTextSize);
    }

    public void setCenterTextColor(int centerTextColor) {
        tv_center.setTextColor(centerTextColor);
    }

    public void setLeftText(String leftText) {
        tv_left.setText(leftText);
        setLeftVisible(true);
    }

    public void setLeftTextSize(float leftTextSize) {
        tv_left.setTextSize(leftTextSize);
    }

    public void setLeftTextColor(int leftTextColor) {
        tv_left.setTextColor(leftTextColor);
    }

    public void setLeftImage(Drawable leftImage) {
        iv_left.setImageDrawable(leftImage);
        iv_left.setVisibility(VISIBLE);
        setLeftVisible(true);
    }
    public void setLeftImage(int leftImage) {
        iv_left.setImageResource(leftImage);
        iv_left.setVisibility(VISIBLE);
        setLeftVisible(true);
    }

    public void setRightText(String rightText) {
        tv_right.setText(rightText);
        setRightVisible(true);

    }

    public void setRightTextSize(float rightTextSize) {
        tv_right.setTextSize(rightTextSize);
    }

    public void setRightTextColor(int rightTextColor) {
        tv_right.setTextColor(rightTextColor);
    }

    public void setRightImage(Drawable rightImage) {
        iv_right.setImageDrawable(rightImage);
        iv_right.setVisibility(VISIBLE);
        setRightVisible(true);
    }
    public void setRightImage(int rightImage) {
        iv_right.setImageResource(rightImage);
        iv_right.setVisibility(VISIBLE);
        setRightVisible(true);
    }

    private void setLeftVisible(boolean leftVisible) {
        if (!leftVisible) {
            ll_left.setVisibility(INVISIBLE);
        } else {
            ll_left.setVisibility(VISIBLE);
        }
    }

    private void setRightVisible(boolean rightVisible) {
        if (!rightVisible) {
            ll_right.setVisibility(INVISIBLE);
        } else {
            ll_right.setVisibility(VISIBLE);
        }
    }

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        imageSize = ScreenUtil.dip2px(context, 20);
        isInEditMode();
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.titleBar);

        barBackgroundDrawable = ta.getDrawable(R.styleable.titleBar_barBackground);
        barBackgroundColor = ta.getColor(R.styleable.titleBar_barBackground,
                context.getResources().getColor(R.color.black));

        centerText = ta.getString(R.styleable.titleBar_centerText);
        centerTextColor = ta.getColor(R.styleable.titleBar_centerTextColor,
                context.getResources().getColor(defaultColor));
        centerTextSize = ta.getDimension(R.styleable.titleBar_centerTextSize, 18);

        leftText = ta.getString(R.styleable.titleBar_leftText);
        leftTextColor = ta.getColor(R.styleable.titleBar_leftTextColor,
                context.getResources().getColor(defaultColor));
        leftTextSize = ta.getDimension(R.styleable.titleBar_leftTextSize, 12);
        leftImage = ta.getDrawable(R.styleable.titleBar_leftImage);

        rightText = ta.getString(R.styleable.titleBar_rightText);
        rightTextColor = ta.getColor(R.styleable.titleBar_rightTextColor,
                context.getResources().getColor(defaultColor));
        rightTextSize = ta.getDimension(R.styleable.titleBar_rightTextSize, 12);
        rightImage = ta.getDrawable(R.styleable.titleBar_rightImage);

        ta.recycle();

        //左布局
        ll_left = new LinearLayout(context);
        ll_left.setOrientation(HORIZONTAL);
        ll_left.setGravity(Gravity.CENTER);
        ll_left.setPadding(padding16, 0, padding8, 0);
        LayoutParams leftLayoutParams = new LayoutParams(imageSize * 4, ViewGroup.LayoutParams.MATCH_PARENT);
        ll_left.setLayoutParams(leftLayoutParams);
        //左文字
        tv_left = new TextView(context);
        tv_left.setText(leftText);
        tv_left.setTextSize(leftTextSize);
        tv_left.setPadding(padding8, 0, padding8, 0);
        tv_left.setTextColor(leftTextColor);
        tv_left.setGravity(Gravity.CENTER);
        LayoutParams leftTextLayoutParams = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1);
        tv_left.setLayoutParams(leftTextLayoutParams);
        //左图标
        iv_left = new ImageView(context);
        if (leftImage != null) {
            iv_left.setImageDrawable(leftImage);
            iv_left.setVisibility(VISIBLE);
        } else {
            iv_left.setVisibility(INVISIBLE);
        }
        LayoutParams leftImageLayoutParams = new LayoutParams(imageSize, imageSize);
        iv_left.setLayoutParams(leftImageLayoutParams);
        //左布局添加左文字,左图标
        ll_left.addView(iv_left);
        ll_left.addView(tv_left);
        if (!StringUtil.isBlank(leftText) || null != leftImage) {
            ll_left.setVisibility(VISIBLE);
        } else {
            ll_left.setVisibility(INVISIBLE);
        }

        //右布局
        ll_right = new LinearLayout(context);
        ll_right.setOrientation(HORIZONTAL);
        ll_right.setGravity(Gravity.CENTER);
        ll_right.setPadding(padding8, 0, padding16, 0);
        LayoutParams rightLayoutParams = new LayoutParams(imageSize * 4, ViewGroup.LayoutParams.MATCH_PARENT);
        ll_right.setLayoutParams(rightLayoutParams);
        //右文字
        tv_right = new TextView(context);
        tv_right.setText(rightText);
        tv_left.setPadding(padding8, 0, padding8, 0);
        tv_right.setTextSize(rightTextSize);
        tv_right.setTextColor(rightTextColor);
        tv_right.setGravity(Gravity.CENTER);
        LayoutParams rightTextLayoutParams = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1);
        tv_right.setLayoutParams(rightTextLayoutParams);

        //右图标
        iv_right = new ImageView(context);
        if (rightImage != null) {
            iv_right.setImageDrawable(rightImage);
            iv_right.setVisibility(VISIBLE);
        } else {
            iv_right.setVisibility(INVISIBLE);
        }
        LayoutParams rightImageLayoutParams = new LayoutParams(imageSize, imageSize);
        iv_right.setLayoutParams(rightImageLayoutParams);
        //右布局添加右文字,右图标
        ll_right.addView(tv_right);
        ll_right.addView(iv_right);
        if (!StringUtil.isBlank(rightText) || null != rightImage) {
            ll_right.setVisibility(VISIBLE);
        } else {
            ll_right.setVisibility(INVISIBLE);
        }
        //标题文字
        tv_center = new TextView(context);
        tv_center.setGravity(Gravity.CENTER);
        tv_center.setMaxLines(1);
        tv_center.setText(centerText);
        tv_center.setTypeface(Typeface.DEFAULT_BOLD);
        tv_center.setTextSize(centerTextSize);
        tv_center.setTextColor(centerTextColor);
        LayoutParams centerLayoutParams = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1);
        tv_center.setLayoutParams(centerLayoutParams);
        //总布局
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            this.setBackground(barBackgroundDrawable);
        } else {
            this.setBackgroundDrawable(barBackgroundDrawable);
        }
        this.setBackgroundColor(barBackgroundColor);
        this.addView(ll_left);
        this.addView(tv_center);
        this.addView(ll_right);


    }

    public void setLeftClick(OnClickListener onClickListener) {
        ll_left.setOnClickListener(onClickListener);
    }

    public void setRightClick(OnClickListener onClickListener) {
        ll_right.setOnClickListener(onClickListener);
    }

    public void initBar(final Activity context, String title){
        setCenterText(title);
        setLeftImage(R.mipmap.arrow_left_white_light);
        setLeftClick(new OnClickListener() {
            @Override
            public void onClick(View v) {
                context.finish();
            }
        });
    }


}
